<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\CarVinLog;

class VinLogController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //

    /**
     * 获取brands
     */
    public function getList( Request $request ){

        $res = [];
        $get = $request->all();
        $page = isset( $get['page'] )? intval($get['page']) : 1;
        $number = isset( $get['number'] )? intval($get['number']) : 20;
        $where = [];

        if( !empty( $get['vin'] ) ){
            $where[] =[ 'vin', 'like', '%' . $get['vin'] .'%'];
        }

        if( !empty( $get['country'] ) ){
            $where[] =[ 'country', 'like', '%' . $get['country'] .'%'];
        }

        if( !empty( $get['province'] ) ){
            $where[] =[ 'province', 'like', '%' . $get['province'] .'%'];
        }

        if( !empty( $get['city'] ) ){
            $where[] =[ 'city', 'like', '%' . $get['city'] .'%'];
        }

        if( !empty( $get['created_at'] ) ){
            $where[] =[ 'created_at', 'like', '%' . $get['created_at'] .'%'];
        }

        if( !empty( $get['ip'] ) ){
            $where[] =[ 'ip', 'like', '%' . $get['ip'] .'%'];
        }

        $list = CarVinLog::where( $where )
            ->forPage($page, $number)
            ->orderBy('created_at', 'desc')
//            ->select( )
        ->get();

        $count = CarVinLog::where( $where )
            ->count();

        $res = [
            'status'=>0,
            'total'=> $count,
            'page_total'=> ceil( $count / $number),
            'list'=> $list
        ];
        echo json_encode( $res );
    }

}
